package com.shanghaionstar.user.presenter;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.user.PlateAndEngineEnum;
import com.shanghaionstar.user.iview.ISetPlateNumberView;
import com.shanghaionstar.user.service.PlateEngineService;
import com.shanghaionstar.utils.ApplicationUtil;

/**
 * Created by onstarios on 16/6/30.
 */
public class SetPlateNumberPresenter extends BasePresenter<PlateEngineNumEntity> {
    private ISetPlateNumberView plateNumberView;
    private PlateEngineService service;

    public SetPlateNumberPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }


    public void setView(IMVPView<PlateEngineNumEntity> view) {
        super.setView(view);
        plateNumberView = (ISetPlateNumberView) view;
        service = new PlateEngineService(plateNumberView.onActivity());
        setServiceProvider(service);
    }


    public void popAnimationWithKeyboard(final boolean isOpen) {
        Animation animation = AnimationUtils.loadAnimation(view.onActivity(), isOpen ? R.anim.bottom_enter : R.anim.bottom_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                plateNumberView.popAnimationWithKyeboard(animation, isOpen);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void updateVehicleInfo(String plateEngineNumber,String plateEngineFlag){
        PlateEngineNumEntity plateEngineNumEntity=new PlateEngineNumEntity();
        plateEngineNumEntity.setAccountID(ApplicationUtil.getInstance().getAccountId());
        plateEngineNumEntity.setVin(ApplicationUtil.getInstance().getVin());
        if(!CommonUtil.isNull(plateEngineFlag)){
            if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.PlateNumber.name())) {
                plateEngineNumEntity.setLicensePlate(plateEngineNumber);
            } else if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.EngineNumber.name())){
                plateEngineNumEntity.setEngineNumber(plateEngineNumber);
            }
        }
        service.setPlateEngineNumEntity(plateEngineNumEntity,false);
    }
}
